package org.dtrd.modules.doctor.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.doctor.entity.bean.TeamInfo;
import org.dtrd.modules.doctor.entity.po.DtrdEntRdDoctorTeam;
import org.dtrd.modules.doctor.entity.po.DtrdRlDoctorTeamDoctor;
import org.dtrd.modules.doctor.mapper.DtrdEntRdDoctorTeamMapper;
import org.dtrd.modules.doctor.service.IDtrdEntRdDoctorService;
import org.dtrd.modules.doctor.service.IDtrdEntRdDoctorTeamService;
import org.dtrd.modules.doctor.service.IDtrdRlDoctorTeamDoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-11-30 16:48:37
 */
@Service
@DS("multi-datasource1")
public class DtrdEntRdDoctorTeamServiceImpl extends ServiceImpl<DtrdEntRdDoctorTeamMapper, DtrdEntRdDoctorTeam> implements IDtrdEntRdDoctorTeamService {

    @Autowired
    private IDtrdEntRdDoctorService doctorService;
    @Autowired
    private IDtrdRlDoctorTeamDoctorService doctorTeamRlService;

    @Override
    public boolean hasDoctorTeam(Integer doctorId) {
        LambdaQueryWrapper<DtrdEntRdDoctorTeam> queryWrapper = Wrappers.lambdaQuery(DtrdEntRdDoctorTeam.class)
                .eq(DtrdEntRdDoctorTeam::getManageDoctorId, doctorId);
        DtrdEntRdDoctorTeam doctorTeam = this.getOne(queryWrapper);
        return Optional.ofNullable(doctorTeam).isPresent();
    }

    @Override
    public DtrdEntRdDoctorTeam createDoctorTeamByDoctorId(Integer doctorId) {
        DtrdEntRdDoctorTeam doctorTeam = new DtrdEntRdDoctorTeam();
        doctorTeam.setManageDoctorId(doctorId);
        // 获取医生名字
        String doctorName = doctorService.getDoctorName(doctorId);
        if (StrUtil.isNotBlank(doctorName)) {
            doctorTeam.setName(doctorName + "的团队");
        }
        save(doctorTeam);
        return doctorTeam;
    }

    @Override
    public List<Integer> getTeamDoctorIds(Integer teamId) {
        LambdaQueryWrapper<DtrdRlDoctorTeamDoctor> wrapper = Wrappers.lambdaQuery(DtrdRlDoctorTeamDoctor.class)
                .eq(DtrdRlDoctorTeamDoctor::getTeamId, teamId);
        List<DtrdRlDoctorTeamDoctor> teamDoctors = doctorTeamRlService.list(wrapper);
        if (CollectionUtil.isNotEmpty(teamDoctors)) {
            List<Integer> doctorIds = teamDoctors.stream().map(DtrdRlDoctorTeamDoctor::getDoctorId)
                    .collect(Collectors.toList());
            // 加上管理者的id
            doctorIds.add(teamDoctors.get(0).getManageDoctorId());
            return doctorIds;
        }
        return null;
    }

    @Override
    public DtrdEntRdDoctorTeam getDoctorTeamByDoctorId(Integer doctorId) {
        return getByManageDoctorId(doctorId);
    }

    @Override
    public Integer getManagerId(Integer teamId) {
        DtrdEntRdDoctorTeam doctorTeam = getById(teamId);
        return Optional.ofNullable(doctorTeam).map(DtrdEntRdDoctorTeam::getManageDoctorId).orElse(null);
    }

    @Override
    public Date getDoctorTeamCreateTime(Integer doctorId) {
        DtrdEntRdDoctorTeam doctorTeam = getByManageDoctorId(doctorId);
        return Optional.ofNullable(doctorTeam).map(DtrdEntRdDoctorTeam::getCreateTime).orElse(null);
    }

    @Override
    public boolean saveOrUpdateDoctorTeam(TeamInfo info) {
        DtrdEntRdDoctorTeam po = new DtrdEntRdDoctorTeam().parseFromDTO(info);
        return saveOrUpdate(po);
    }

    private DtrdEntRdDoctorTeam getByManageDoctorId(Integer doctorId) {
        LambdaQueryWrapper<DtrdEntRdDoctorTeam> wrapper = Wrappers.lambdaQuery(DtrdEntRdDoctorTeam.class)
                .eq(DtrdEntRdDoctorTeam::getIsDel,0)
                .eq(DtrdEntRdDoctorTeam::getManageDoctorId, doctorId);
        return getOne(wrapper);
    }
}
